package spring.demo.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import spring.demo.model.Student;

@Controller
public class DemoController {

	@RequestMapping(value="/home", method=RequestMethod.GET)
	public ModelAndView handleRequest(
			HttpServletRequest request, HttpServletResponse response){
		
		Student student = new Student();
		student.setName("Coder Son Trang");
		
		List<String> books = new ArrayList<String>();
		books.add("book 1");
		books.add("book 2");
		student.setBooks(books);
		
		ModelAndView mav = new ModelAndView("index.jsp", "modelx", student);
		
		return mav;
	}
	
	@RequestMapping(value="/submitStudentInfo", method=RequestMethod.POST)
	public ModelAndView submitStudentInfo(ModelMap model,
			@ModelAttribute("model")Student student){
		
		List<String> books = student.getBooks();
		String message = "Update success !!!";
		
		model.addAttribute("message", message);
		ModelAndView mav = new ModelAndView("index.jsp", "modelx", student);
		return mav;
	}
}
